package com.scwatch.core.web.back;

import com.scwatch.common.orm.RowSide;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Program;
import com.scwatch.core.domain.ProgramBackcode;
import com.scwatch.core.domain.ProgramDatecheack;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.ProgramBackCodeService;
import com.scwatch.core.service.ProgramDatecheckService;
import com.scwatch.core.service.ProgramService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

/**
 * 回看日期
 * Created by rh006 on 2017/7/27.
 */

@Controller
@RequestMapping("/core/programbackcode")
public class ProgramBackcodeController {

    private static final Logger logger = LoggerFactory
            .getLogger(ProgramBackcodeController.class);

    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:list")
    public String list(
//            Integer id,
            @PageableDefault(sort = "time", direction = Sort.Direction.ASC) Pageable pageable,
            Integer programbackId,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);

        // search_EQ_programDatecheack.id=?

        Page<ProgramBackcode> pagedList = programBackCodeService.findAll(params, pageable);

//        Page<ProgramBackcode> pagedList = programBackCodeService.findAllByProgramDatecheckId(id,params, pageable);

        modelMap.addAttribute("pagedList", pagedList);
        modelMap.addAttribute("proprogramDatecheackId", request.getParameter("search_EQ_programDatecheack.id"));
        modelMap.addAttribute("programbackId",programbackId);
        return "core/program/programbackcode_list";
    }

    @RequestMapping("create.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:create")
    public String create(Integer id, org.springframework.ui.Model modelMap,Integer programbackId) {
        if (id != null) {
            ProgramBackcode bean = programBackCodeService.get(id);
            modelMap.addAttribute("bean", bean);
        }

        // Program 列表
        List<Program> list = programService.findAll();

        modelMap.addAttribute("programList", list);
        modelMap.addAttribute("programbackId",programbackId);
        modelMap.addAttribute(OPRT, CREATE);
        return "core/program/programbackcode_form";
    }

    @RequestMapping("edit.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:edit")
    public String edit(
            Integer id,
            Integer position,
            Integer programbackId,
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        ProgramBackcode bean = programBackCodeService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        RowSide<ProgramBackcode> side = programBackCodeService.findSide(params, bean, position,
                pageable.getSort());
        modelMap.addAttribute("bean", bean);
        modelMap.addAttribute("side", side);
        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        modelMap.addAttribute("programbackId",programbackId);
        return "core/program/programbackcode_form";
    }

    @RequestMapping("save.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:save")
    public String save(ProgramBackcode bean, String redirect,Integer programbackId,
                       HttpServletRequest request, RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();
//        bean.setTime();
//        bean.setUser(user);

        // search_EQ_programDatecheack.id

        ProgramDatecheack programDatecheack = programDatecheckService.get(Integer.valueOf(request.getParameter("search_EQ_programDatecheack.id")));

        bean.setProgramDatecheack(programDatecheack);


        bean.setUser(user);
        programBackCodeService.save(bean, siteId, user);
        logService.operation("opr.programBackcode.add", bean.getName(), null,
                bean.getId(), request);
        logger.info("save ProgramBackcode, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        ra.addAttribute("programbackId",programbackId);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute(bean.getId());
            return "redirect:edit.do";
        }
    }

    @RequestMapping("update.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:update")
    public String update(@ModelAttribute("bean") ProgramBackcode bean,Integer programbackId,
                         Integer position, String redirect, HttpServletRequest request,
                         RedirectAttributes ra) {
        programBackCodeService.update(bean);
        logService.operation("opr.programBackcode.edit", bean.getName(), null,
                bean.getId(), request);
        logger.info("update ProgramBackcode, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        ra.addAttribute("programbackId",programbackId);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else {
            ra.addAttribute("id", bean.getId());
            ra.addAttribute("position", position);
            return "redirect:edit.do";
        }
    }

    @RequestMapping("delete.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:programbackcode:delete")
    public String delete(Integer[] ids, HttpServletRequest request,Integer programbackId,
                         RedirectAttributes ra) {
        ProgramBackcode[] beans = programBackCodeService.delete(ids);
        for (ProgramBackcode bean : beans) {
            logService.operation("opr.programBackcode.delete", bean.getName(),
                    null, bean.getId(), request);
            logger.info("delete ProgramBackcode, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        ra.addAttribute("programbackId",programbackId);
        return "redirect:list.do";
    }

    @ModelAttribute("bean")
    public ProgramBackcode preloadBean(@RequestParam(required = false) Integer oid) {
        return oid != null ? programBackCodeService.get(oid) : null;
    }

    @Autowired
    private OperationLogService logService;
    @Autowired
    private ProgramBackCodeService programBackCodeService;
    @Autowired
    private ProgramDatecheckService programDatecheckService;
    @Autowired
    private ProgramService programService;

}
